<template>
  <div>
    <el-table :data="dataList" 
		:header-cell-style="{'text-align':'center'}"
    :cell-style="{'text-align':'center'}"
		style="width: 100%" row-key="commentId">
    <!-- <el-table-column prop="commentId" label="序号" width="150" /> -->
		<!-- <el-table-column  label="序号" width="100">
      <template #default="scope">
				<span>{{scope.$index+1}}</span>
      </template>
    </el-table-column> -->
		<el-table-column
                fixed
                type="index"
                :index="indexMethod"
                label="序号"
                width="80"
              />
   
    <!-- <el-table-column prop="commentContent" label="内容" width="560" /> -->
		<el-table-column  label="内容" width="500">
      <template #default="scope">
				<div
					style="width: 100%;
						 white-space: nowrap; /* 不换行 */
            overflow: hidden; /* 溢出部分隐藏 */
            text-overflow: ellipsis; /* 显示省略号 */"
				>
					  {{ scope.row.commentContent }}
				</div>
      </template>
    </el-table-column>
		<el-table-column prop="likeCount" label="点赞数" width="120" />
    <el-table-column prop="commentCreateTime" label="时间" width="200" />

    <el-table-column fixed="right" label="操作" width="350">
      <template #default="scope">
        <el-button type="primary" @click="visibleClick(scope.row.commentContent)"  
          >查看详情</el-button
        >
				<el-button type="danger" @click="deleteClick(scope.row.commentId)"   v-hasPerm="['resource:comment:del']"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页搜索 -->
  <div
    style="
      display: flex;
      justify-content: center;
      width: 100%;
      margin-top: 15px;
    "
  >
    <el-pagination
      background
      layout="sizes, prev,slot, next, jumper"
      :page-count="pageTotal"
      :page-sizes="[10, 20, 30, 50]"
      :current-page="activePager"
      @update:current-page="pageChange"
      @update:page-size="sizeChange"
      :default-current-page="1"
      :default-page-size="10"
    >
      <div class="pagination">
        <span
          class="pagination__item"
          :class="{ active: activePager === pager }"
          v-for="pager in pageArr"
          :key="pager"
          @click="pageChange(pager)"
        >
          {{ pager }}
        </span>
      </div>
    </el-pagination>
  </div>
  <!-- 分页搜索 -->

	<el-dialog v-model="conmentVisible" title="评论详情">
		<el-card>
			<div>
				{{ currentConment }}
			</div>
 	  </el-card>
		 <template #footer>
      <span class="dialog-footer">
        <el-button @click="conmentVisible = false">关闭</el-button>
      </span>
    </template>
  </el-dialog>

  </div>
</template>

<script lang="ts" setup>
import { getComment, deleteComment } from "@/api/comment";
import { getListInfo, getPage } from "@/api/comment/type";
import { ref, onMounted, reactive } from "vue";
const loading = ref(false);
const queryParams = reactive<getPage>({
  pageNum: 1,
  pageSize: 10,
});

const dataList = ref<getListInfo[]>();

//总共页数
const pageTotal = ref(0);
//后端传来的页码数组
const pageArr = ref([]);
//当前页码
const activePager = ref(1);

const indexMethod = (index: number) => {
  return index + (activePager.value - 1) * queryParams.pageSize + 1;
};

const conmentVisible = ref(false);
//评论详情
const currentConment = ref('');

function visibleClick( conment:string) {
	currentConment.value = conment;
	conmentVisible.value = true;
}


//删除
async function deleteClick(id: number) {
  await ElMessageBox.confirm("确认删除用户?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    deleteComment(id).then(() => {
      ElMessage.success("删除成功");
      updateInfo();
    });
  });
}

//获取数据列表
async function handleData() {
  loading.value = true;
  await getComment(queryParams)
    .then(({ data }) => {
      dataList.value = data.comments;
      pageTotal.value = data.navigatepageNums[data.navigatepageNums.length - 1];
      pageArr.value = data.navigatepageNums;
    })
    .catch((error) => {
      console.error("Error:", error);
    })
    .finally(() => {
      loading.value = false;
    });
}

function updateInfo() {
  // queryFormRef.value.resetFields();
  handleData();
}

function pageChange(pageNum: number) {
  queryParams.pageNum = pageNum;
  activePager.value = pageNum;
  handleData();
}

function sizeChange(sizeNum: number) {
  queryParams.pageSize = sizeNum;
  handleData();
}

onMounted(() => {
  handleData();
});
</script>

<style scoped>
.pagination {
  display: flex;
  justify-content: center;
}

.pagination__item {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  margin: 0 5px;
  cursor: pointer;
  border: 1px solid #ccc;
}

.pagination__item.active {
  color: white;
  background-color: #4593ff;
}
</style>
